package com.botao.dao;

import com.botao.pojo.Classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @deprecated 已经由mybatis实现，不再使用jdbc模板。
 */
@Deprecated
@Repository("jdbcClassesDao")
public class ClassesDaoImpl implements ClassesDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void save(Classes classes) {
        jdbcTemplate.update("insert into classes value (?,?,?,?,?)",
                null,
                classes.getNianFen(),
                classes.getZhuanYe(),
                classes.getBanJi(),
                classes.getXueYuan()
        );
    }

    @Override
    public List<Classes> listClasses(Long currentPage, Long pageSize) {
        long start = (currentPage - 1) * pageSize;
        return jdbcTemplate.query("select * from classes group by zhuanYe,id limit " + start + " ," + pageSize, new BeanPropertyRowMapper<Classes>(Classes.class));
    }

    @Override
    public void delete(int id) {
        jdbcTemplate.update("delete from classes where id = ?", id);
    }

    @Override
    public Classes getOne(int id) {
        List<Classes> query = jdbcTemplate.query("select * from classes where id = ?", new BeanPropertyRowMapper<Classes>(Classes.class), id);
        return query.get(0);
    }

    @Override
    public void update(Classes classes) {
        jdbcTemplate.update("update classes set nianFen = ? ,zhuanYe = ? , banJi = ? , xueYuan = ? where id = ?",
                classes.getNianFen(),
                classes.getZhuanYe(),
                classes.getBanJi(),
                classes.getXueYuan(),
                classes.getId()
        );
    }

    @Override
    public Long count() {
        return jdbcTemplate.queryForObject("select COUNT(*) from classes", Long.class);
    }

    @Override
    public List<Classes> getAllClasses() {
        return jdbcTemplate.query("select * from classes", new BeanPropertyRowMapper<Classes>(Classes.class));
    }

    @Override
    public int getClassesIdByBanJi(String banJi) {
        return jdbcTemplate.queryForObject("select id from classes where banJi = ?", Integer.class, banJi);
    }
}
